package com.quanyan.travel.component.task;

import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.jobclient.JobClient;
import com.github.ltsopensource.jobclient.domain.Response;
import com.quanyan.travel.constants.OutdoorConstants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Since 2016-07-04
 */
public class TimerTaskJob {

    private Logger logger = Logger.getLogger(TimerTaskJob.class);

    @Autowired
    private JobClient jobClient;

    @Value("${lts.node.name}")
    private String ltsNodeName;

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void executeTask() {
        // 每隔一定时间来刷新仓库,关闭未进行支付的订单
        Job clubIMgroupidCheckJob = new Job();
        clubIMgroupidCheckJob.setTaskId(OutdoorConstants.TRAVEL_JOB_ID_CLOSE_ORDER_START_NOTIFY);
        clubIMgroupidCheckJob.setTaskTrackerNodeGroup(this.ltsNodeName); //必须跟配置文件统一
        clubIMgroupidCheckJob.setNeedFeedback(true);
        clubIMgroupidCheckJob.setReplaceOnExist(true);        // 当任务队列中存在这个任务的时候，是否替换更新
        clubIMgroupidCheckJob.setRepeatCount(-1);             // 无限循环
        clubIMgroupidCheckJob.setRepeatInterval(5 * 60 * 1000L);  // 5分钟 执行一次
        Response notPayActivityOrderRes = jobClient.submitJob(clubIMgroupidCheckJob);
        logger.warn("回收不合法订单，返回结果: " + notPayActivityOrderRes);

        // 每隔一定时间来更新活动的状态,定时刷新已经下架的非季度活动,同时下架非季度活动下的场次
        Job statsClubIntegral = new Job();
        statsClubIntegral.setTaskId(OutdoorConstants.TRAVEL_JOB_ID_UPDATE_ACTIVITY_START_NOTIFY);
        statsClubIntegral.setTaskTrackerNodeGroup(this.ltsNodeName); //必须跟配置文件统一
        statsClubIntegral.setParam("type", "statsClubIntegral");
        statsClubIntegral.setNeedFeedback(true);
        statsClubIntegral.setReplaceOnExist(true); // 当任务队列中存在这个任务的时候，是否替换更新
        statsClubIntegral.setRepeatCount(-1);  //无限循环
        statsClubIntegral.setRepeatInterval(10 * 60 * 1000L); //10分钟 执行一次
        Response statsClubIntegralResp = jobClient.submitJob(statsClubIntegral);
        logger.warn("清除过期活动，返回结果: " + statsClubIntegralResp);

        //更新最低价格和开始时间
        Job weekRangeJob = new Job();
        weekRangeJob.setTaskId(OutdoorConstants.TRAVEL_JOB_ID_UPDATE_PRICE_START_NOTIFY);
        weekRangeJob.setTaskTrackerNodeGroup(this.ltsNodeName); //必须跟配置文件统一
        weekRangeJob.setParam("type", "statsClubIntegral");
        weekRangeJob.setNeedFeedback(true);
        weekRangeJob.setReplaceOnExist(true); // 当任务队列中存在这个任务的时候，是否替换更新
        weekRangeJob.setRepeatCount(-1);  //无限循环
        weekRangeJob.setRepeatInterval(5 * 60 * 1000L);  // 5分钟 执行一次
        Response response = jobClient.submitJob(weekRangeJob);
        logger.warn("更新最低价格和开始时间，返回结果: " + response);

        //每隔分钟刷新一下提醒用户付款
        Job updateActivityStatusJob = new Job();
        updateActivityStatusJob.setTaskId(OutdoorConstants.TRAVEL_JOB_ID_REMIND_PAY_START_NOTIFY);
        updateActivityStatusJob.setTaskTrackerNodeGroup(this.ltsNodeName); //必须跟配置文件统一
        updateActivityStatusJob.setParam("type", "statsClubIntegral");
        updateActivityStatusJob.setNeedFeedback(true);
        updateActivityStatusJob.setReplaceOnExist(true); // 当任务队列中存在这个任务的时候，是否替换更新
        updateActivityStatusJob.setRepeatCount(-1);  //无限循环
        updateActivityStatusJob.setRepeatInterval(10 * 60 * 1000L);  // 10分钟 执行一次
        Response statusResponse = jobClient.submitJob(updateActivityStatusJob);
        logger.warn("支付提醒，返回结果: " + statusResponse);
    }

    /**
     * 提交一个定时任务，指定时间执行
     *
     * @param params      传递给业务执行类的参数
     * @param taskId      任务id
     * @param executeTime 执行时间点
     */
    public void submitTask(Map<String, String> params, String taskId,
                           long executeTime, long repeatInterval) {
        logger.warn("executeTime=" + sf.format(new Date(executeTime)) + ", repeatInterval=" + repeatInterval);
        Job job = new Job();
        job.setTaskId(taskId);
        job.setTaskTrackerNodeGroup("taskTracker_club");
        job.setNeedFeedback(true);
        job.setReplaceOnExist(true);        // 当任务队列中存在这个任务的时候，是否替换更新
        job.setTriggerTime(executeTime);    //设置指定时间执行，在指定时间点触发
        if (params != null && params.size() > 0) {
            job.setExtParams(params);           //执行任务用到的参数
        }
        if (repeatInterval != 0) {
            job.setRepeatCount(-1); //无限循环
            job.setRepeatInterval(repeatInterval); //每间隔多久执行一次，单位秒
        }
        Response response = jobClient.submitJob(job);

        logger.warn("启动定时任务：" + taskId + ", 参数：" + params +
                ", 在" + sf.format(new Date(executeTime)) + "时执行！" + " 返回结果: " + response);
    }
}
